package com.driving.single.common.exception;

import com.driving.single.common.entity.DrivingResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局业务异常处理
 *
 * @author xsfno1
 * @version v1.0.0
 * @date 2020/2/20 17:30
 * @see com.driving.single.common.exception
 **/
@ControllerAdvice
public class DrivingExceptionHandler {
    @ExceptionHandler(DrivingException.class)
    public ResponseEntity<?> handlerException(HttpServletRequest request, Exception ex) {
        DrivingResult error = new DrivingResult();
        // 业务异常
        if (ex instanceof DrivingException) {
            error.setStatus(((DrivingException) ex).getStatus());
            error.setMessage(ex.getMessage());
        }
        return new ResponseEntity<>(error, HttpStatus.OK);
    }
}
